package com.xhwl.logistics.service.impl.http;

import com.alibaba.fastjson.JSON;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.dto.http.*;
import com.xhwl.logistics.dto.http.query.QueryAccessRecordNanTouDTO;
import com.xhwl.logistics.dto.http.query.QueryAccessRecordStatisticsNanTouDTO;
import com.xhwl.logistics.dto.http.query.QueryCarDTO;
import com.xhwl.logistics.dto.http.query.QueryCarportNanTouDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.List;
import java.util.Objects;

@Slf4j
//@Service
public class NanTouHttpServiceImpl {

    @Autowired
    private RestTemplate restTemplate;

    private String nanTouResponseHandle(ResponseEntity<ResponseCommonNanTouDTO> response) {
        if (200 != response.getStatusCode().value()) {
            throw new BusinessException("请求南投车场WEB服务，http相应状态码不是200");
        }
        ResponseCommonNanTouDTO body = response.getBody();
        if (Objects.isNull(body)) {
            throw new BusinessException("请求南投车场WEB服务，http没有响应体");
        }
        if (!"200".equals(body.getCode())) {
            throw new BusinessException("请求南投车场WEB服务，返回的业务状态码不是200");
        }
        String data = body.getData();
        if (Objects.isNull(data)) {
            throw new BusinessException("请求南投车场WEB服务，返回的业务数据data为null");
        }
        return data;
    }

    public CarportNanTouDTO queryCarport(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryCarportNanTouDTO queryCarportNanTouDTO) {
        final String path = "/nan-tou/carport";
        final String host = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHost();

        URI uri = UriComponentsBuilder.fromHttpUrl(host + path).build().toUri();
        ResponseEntity<ResponseCommonNanTouDTO> response = restTemplate.postForEntity(uri, queryCarportNanTouDTO, ResponseCommonNanTouDTO.class);

        return JSON.parseObject(nanTouResponseHandle(response), CarportNanTouDTO.class);
    }

    public AccessRecordStatisticsNanTouDTO queryAccessRecordStatistics(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryAccessRecordStatisticsNanTouDTO queryAccessRecordStatisticsNanTouDTO) {
        final String path = "/nan-tou/access-record/statistics/recently";
        final String host = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHost();

        URI uri = UriComponentsBuilder.fromHttpUrl(host + path).build().toUri();
        ResponseEntity<ResponseCommonNanTouDTO> response = restTemplate.postForEntity(uri, queryAccessRecordStatisticsNanTouDTO, ResponseCommonNanTouDTO.class);
        return JSON.parseObject(nanTouResponseHandle(response), AccessRecordStatisticsNanTouDTO.class);
    }

    public List<AccessRecordNanTouDTO> queryAccessRecords(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryAccessRecordNanTouDTO queryAccessRecordNanTouDTO) {
        final String path = "/nan-tou/access-record/recently";
        final String host = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHost();

        URI uri = UriComponentsBuilder.fromHttpUrl(host + path).build().toUri();
        ResponseEntity<ResponseCommonNanTouDTO> response = restTemplate.postForEntity(uri, queryAccessRecordNanTouDTO, ResponseCommonNanTouDTO.class);
        return JSON.parseArray(nanTouResponseHandle(response), AccessRecordNanTouDTO.class);
    }

    public List<CarNanTouDTO> queryCarList(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryCarDTO queryCarDTO){
        final String path = "/nan-tou//car/car-card";
        final String host = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHost();
        URI uri = UriComponentsBuilder.fromHttpUrl(host + path).build().toUri();
        ResponseEntity<ResponseCommonNanTouDTO> response = restTemplate.postForEntity(uri, queryCarDTO, ResponseCommonNanTouDTO.class);
        return JSON.parseArray(nanTouResponseHandle(response), CarNanTouDTO.class);
    }

}
